********Clean and compile data needed for analysis from MxFLS3*********

***Clean Book C, which contains individuals' roster and education information
cd "$mxfls3rawdata/hh09dta_bc"
use c_ls, clear
duplicates drop folio pid_link, force
destring ls, replace
egen hhsize=max(ls), by(folio)
keep folio pid_link hhsize ls04 ls14

*Information in location
merge m:1 folio using c_portad, gen(_mergecportad) keepus(mpio ent)

tempfile bookC_cleanmex09
save "`bookC_cleanmex09'", replace

***Clean Book 3B, which contains risk preference questions, smoking
cd "$mxfls3rawdata/hh09dta_b3b"
use iiib_portad.dta 

*Information on date of interview
preserve
use iiib_conpor, clear
keep if secuencia==1 // first visit
duplicates drop folio pid_link secuencia, force
tempfile b3b109
save "`b3b109'"
restore

merge 1:1 folio pid_link using "`b3b109'", gen(_mergeconpor)

*Attentiveness 
merge 1:1 folio pid_link using iiib_ne.dta, gen(_mergene) keepus(ne02 ne03)

rename ne02 b3bne2
rename ne03 b3bne3

*Smoking behavior
merge 1:1 folio pid_link using iiib_gh.dta, gen(_mergegh) keepus(gh06 gh09_1 gh10_1 gh10_21 gh10_22)

*Risk questions
merge 1:1 folio pid_link using iiib_rg.dta, gen(_mergerg) keepus(rg*)

tempfile book3b_cleanmex09
save "`book3b_cleanmex09'", replace

***Clean Book 3A, which contains work and violence experiences
cd "$mxfls3rawdata/hh09dta_b3a"
use iiia_portad.dta, clear

*Work information - note sector only asked in MxFLS2
merge 1:1 folio pid_link using  iiia_tb.dta, gen(_mergetb) keepus(tb02_1 tb03 tb04 tb05 tb32p)

*Violence experiences
merge 1:1 folio pid_link using iiia_vli.dta, gen(_mergevli) keepus(vli01 vli02 vli05 vli06)

tempfile book3a_cleanmex09
save "`book3a_cleanmex09'", replace

***Clean Book EA, which contains individuals' cognitive measures

cd "$mxfls3rawdata/hh09dta_bea"

use ea_eca, clear

gen correctans1=8
gen correctans2=4
gen correctans3=5
gen correctans4=1
gen correctans5=2
gen correctans6=5
gen correctans7=6
gen correctans8=3
gen correctans9=7
gen correctans10=8
gen correctans11=7
gen correctans12=6

forval num=1/12 {
if `num'<=9 {
local typ 0`num'
} 
if `num'>=10 {
local typ `num'
} 
gen correct`typ'=.
replace correct`typ'=1 if eca`typ'==correctans`num' & eca`typ'!=.
replace correct`typ'=0 if eca`typ'!=correctans`num' & eca`typ'!=.

}

egen totcogscore=rowtotal(correct??), missing

keep folio pid_link totcogscore

tempfile bookea_cleanmex09
save "`bookea_cleanmex09'", replace

***Clean Book 2, which contains HH's experience of events/natural disasters/violence

cd "$mxfls3rawdata/hh09dta_b2"
use ii_portad.dta 

*Natural disasters/shocks experienced by HH
merge 1:1 folio using ii_se.dta, gen(_mergese) keepus(se01d)

*Crime HH (this only has how many times incident happened. for specific incidents details, see vlh2)
merge 1:1 folio using ii_vlh.dta, gen(_mergevlh) keepus(vlh04 vlh10a vlh10b vlh10c vlh11a vlh11b vlh11c)

tempfile book2_cleanmex09
save "`book2_cleanmex09'", replace

***HH income, assets, etc
cd "$reploc/programs"
do clean_mxfls3_balsheet.do
tempfile hhbalsheetfullmex09
save "`hhbalsheetfullmex09'", replace

***Merge all books/information

*Individual
use "`bookC_cleanmex09'", clear
merge 1:1 folio pid_link using "`book3a_cleanmex09'",  gen(_mergeb3a)
merge 1:1 folio pid_link using "`book3b_cleanmex09'",  gen(_mergeb3b)
merge 1:1 folio pid_link using "`bookea_cleanmex09'",  gen(_mergeea)

tempfile individualdatamex09
save "`individualdatamex09'", replace

*Household
use "`book2_cleanmex09'", clear
merge m:1 folio using "`hhbalsheetfullmex09'", gen(_mergebalsheet)
drop if _mergebalsheet!=3

tempfile hhdatamex09
save "`hhdatamex09'"


*Merge individual and HH
use "`individualdatamex09'", clear

*Drop duplicates
sort pid_link
quietly by pid_link:  gen dup = cond(_N==1,0,_n)
drop if dup!=0

merge m:1 folio using "`hhdatamex09'", gen(_mergehh)
drop if _mergehh!=3

*Rename variables to know they are MxFLS3 variables, except for pidlink, which is unique individual identifier across waves
foreach x of varlist _all {
if "`x'"!="pid_link" {
	rename `x' mxfls3_`x'
}
}

gen mxfls=3

*Consistent pidlink
local lets P H 
local i=0
foreach let in `lets' {
local i=`i'+1
if `i'==1{
gen posi1=strpos(pid_link,"`let'")
gen pidi1=substr(pid_link,1,posi1-2)
gen pidi2=substr(pid_link,posi1+1,length(pid_link))
gen pidiint=pidi1+pidi2
}
if `i'==2{
gen posi12=strpos(pid_link,"`let'")
gen pidi12=substr(pid_link,1,posi12-2)
gen pidi22=substr(pid_link,posi12+1,length(pid_link))
gen pidiint2=pidi12+pidi22
}
}

rename pid_link pidlinkold
gen pid_link=pidiint if posi12==0
replace pid_link=pidiint2 if posi12!=0
